package Example1;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
@MultipartConfig
public class Example6 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.从请求中获取登陆密码
        req.setCharacterEncoding("utf8");
        resp.setContentType("text/html; charset=utf8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        // 登录成功
        HttpSession session = req.getSession(true);
        session.setAttribute("username",username);
        resp.sendRedirect("index.html");
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 针对登陆状态进行处理
        HttpSession session = req.getSession(false);
        if (session == null){
            resp.setStatus(403);
            return;
        }
        String username = (String) session.getAttribute("username");
        if (username == null){
            resp.setStatus(403);
            return;
        }
        // 不写也行,默认就是200
        resp.setStatus(200);
    }
}
